"""
能耗分析相关的数据模型
"""
from datetime import datetime
from typing import List, Dict, Optional, Any, Tuple
from pydantic import BaseModel, Field
from enum import Enum


class EnergyType(str, Enum):
    """能耗类型枚举"""
    LIGHTING = "lighting"          # 照明
    HVAC = "hvac"                  # 空调
    EQUIPMENT = "equipment"        # 设备
    VENTILATION = "ventilation"    # 通风
    TOTAL = "total"               # 总能耗


class OptimizationLevel(str, Enum):
    """优化级别枚举"""
    HIGH = "high"        # 高优化
    MEDIUM = "medium"    # 中等优化
    LOW = "low"          # 低优化
    NONE = "none"        # 无优化


class EnergyEfficiencyGrade(str, Enum):
    """能效等级枚举"""
    A_PLUS = "A+"    # A+级
    A = "A"          # A级
    B = "B"          # B级
    C = "C"          # C级
    D = "D"          # D级


class EnergyDataRequest(BaseModel):
    """能耗数据请求"""
    space_id: str = Field(..., description="空间ID")
    energy_types: List[EnergyType] = Field(..., description="能耗类型列表")
    start_time: datetime = Field(..., description="开始时间")
    end_time: datetime = Field(..., description="结束时间")


class EnergyDataResponse(BaseModel):
    """能耗数据响应"""
    data_id: str = Field(..., description="数据ID")
    space_id: str = Field(..., description="空间ID")
    timestamp: datetime = Field(..., description="时间戳")
    energy_type: EnergyType = Field(..., description="能耗类型")
    consumption: float = Field(..., description="消耗量")
    unit: str = Field(..., description="单位")
    cost: float = Field(..., description="成本")
    source: str = Field(..., description="数据源")
    metadata: Dict[str, Any] = Field(default_factory=dict, description="元数据")


class EnergyAnalysisRequest(BaseModel):
    """能耗分析请求"""
    space_id: str = Field(..., description="空间ID")
    energy_types: List[EnergyType] = Field(..., description="能耗类型列表")
    start_time: datetime = Field(..., description="开始时间")
    end_time: datetime = Field(..., description="结束时间")


class EnergyAnalysisResponse(BaseModel):
    """能耗分析响应"""
    analysis_id: str = Field(..., description="分析ID")
    space_id: str = Field(..., description="空间ID")
    analysis_period: Tuple[datetime, datetime] = Field(..., description="分析周期")
    total_consumption: float = Field(..., description="总消耗量")
    consumption_by_type: Dict[str, float] = Field(..., description="按类型分组的消耗量")
    total_cost: float = Field(..., description="总成本")
    cost_by_type: Dict[str, float] = Field(..., description="按类型分组的成本")
    efficiency_grade: EnergyEfficiencyGrade = Field(..., description="能效等级")
    baseline_comparison: Dict[str, float] = Field(..., description="基线对比")
    peak_consumption: Dict[str, Any] = Field(..., description="峰值消耗")
    trends: Dict[str, Any] = Field(..., description="趋势分析")
    optimization_potential: Dict[str, float] = Field(..., description="优化潜力")


class OptimizationRecommendationResponse(BaseModel):
    """优化建议响应"""
    recommendation_id: str = Field(..., description="建议ID")
    space_id: str = Field(..., description="空间ID")
    energy_type: EnergyType = Field(..., description="能耗类型")
    optimization_level: OptimizationLevel = Field(..., description="优化级别")
    title: str = Field(..., description="标题")
    description: str = Field(..., description="描述")
    estimated_savings: Dict[str, float] = Field(..., description="预计节省")
    implementation_cost: float = Field(..., description="实施成本")
    payback_period: float = Field(..., description="回收期（月）")
    priority: int = Field(..., description="优先级")
    actions: List[Dict[str, Any]] = Field(..., description="行动计划")


class EnergyReportRequest(BaseModel):
    """能耗报告请求"""
    report_type: str = Field(..., description="报告类型", pattern="^(daily|weekly|monthly|annual)$")
    start_time: datetime = Field(..., description="开始时间")
    end_time: datetime = Field(..., description="结束时间")
    spaces: List[str] = Field(..., description="空间ID列表")


class EnergyReportResponse(BaseModel):
    """能耗报告响应"""
    report_id: str = Field(..., description="报告ID")
    report_type: str = Field(..., description="报告类型")
    period: Tuple[datetime, datetime] = Field(..., description="报告周期")
    spaces: List[str] = Field(..., description="空间列表")
    analysis_results: List[EnergyAnalysisResponse] = Field(..., description="分析结果")
    recommendations: List[OptimizationRecommendationResponse] = Field(..., description="优化建议")
    summary: Dict[str, Any] = Field(..., description="报告摘要")
    generated_at: datetime = Field(..., description="生成时间")


class EnergyStatisticsRequest(BaseModel):
    """能耗统计请求"""
    space_id: str = Field(..., description="空间ID")
    start_time: datetime = Field(..., description="开始时间")
    end_time: datetime = Field(..., description="结束时间")


class EnergyStatisticsResponse(BaseModel):
    """能耗统计响应"""
    space_id: str = Field(..., description="空间ID")
    period: Tuple[datetime, datetime] = Field(..., description="统计周期")
    total_consumption_kwh: float = Field(..., description="总消耗量(kWh)")
    total_cost_yuan: float = Field(..., description="总成本(元)")
    consumption_by_type: Dict[str, float] = Field(..., description="按类型分组的消耗量")
    analysis_count: int = Field(..., description="分析次数")
    average_efficiency_grade: str = Field(..., description="平均能效等级")


class EnergyOptimizationRequest(BaseModel):
    """能耗优化请求"""
    analysis_id: str = Field(..., description="分析ID")


class EnergyOptimizationResponse(BaseModel):
    """能耗优化响应"""
    recommendations: List[OptimizationRecommendationResponse] = Field(..., description="优化建议列表")
    total_potential_savings: Dict[str, float] = Field(..., description="总节能潜力")
    total_implementation_cost: float = Field(..., description="总实施成本")
    average_payback_period: float = Field(..., description="平均回收期")


class EnergyComparisonRequest(BaseModel):
    """能耗对比请求"""
    space_ids: List[str] = Field(..., description="空间ID列表")
    start_time: datetime = Field(..., description="开始时间")
    end_time: datetime = Field(..., description="结束时间")
    comparison_type: str = Field(..., description="对比类型", pattern="^(consumption|cost|efficiency)$")


class EnergyComparisonResponse(BaseModel):
    """能耗对比响应"""
    comparison_id: str = Field(..., description="对比ID")
    comparison_type: str = Field(..., description="对比类型")
    period: Tuple[datetime, datetime] = Field(..., description="对比周期")
    spaces: List[str] = Field(..., description="空间列表")
    comparison_data: Dict[str, Any] = Field(..., description="对比数据")
    rankings: List[Dict[str, Any]] = Field(..., description="排名")
    insights: List[str] = Field(..., description="洞察")


class EnergyTrendRequest(BaseModel):
    """能耗趋势请求"""
    space_id: str = Field(..., description="空间ID")
    energy_type: Optional[EnergyType] = Field(None, description="能耗类型")
    start_time: datetime = Field(..., description="开始时间")
    end_time: datetime = Field(..., description="结束时间")
    granularity: str = Field(..., description="粒度", pattern="^(hour|day|week|month)$")


class EnergyTrendResponse(BaseModel):
    """能耗趋势响应"""
    space_id: str = Field(..., description="空间ID")
    energy_type: Optional[EnergyType] = Field(None, description="能耗类型")
    period: Tuple[datetime, datetime] = Field(..., description="趋势周期")
    granularity: str = Field(..., description="数据粒度")
    trend_data: List[Dict[str, Any]] = Field(..., description="趋势数据")
    trend_analysis: Dict[str, Any] = Field(..., description="趋势分析")
    forecasting: Optional[Dict[str, Any]] = Field(None, description="预测数据")


class EnergyAlertRequest(BaseModel):
    """能耗告警请求"""
    space_id: str = Field(..., description="空间ID")
    energy_type: EnergyType = Field(..., description="能耗类型")
    threshold_type: str = Field(..., description="阈值类型", pattern="^(consumption|cost|efficiency)$")
    threshold_value: float = Field(..., description="阈值")
    comparison_operator: str = Field(..., description="比较操作符", pattern="^(gt|lt|eq|gte|lte)$")
    alert_enabled: bool = Field(True, description="是否启用告警")


class EnergyAlertResponse(BaseModel):
    """能耗告警响应"""
    alert_id: str = Field(..., description="告警ID")
    space_id: str = Field(..., description="空间ID")
    energy_type: EnergyType = Field(..., description="能耗类型")
    threshold_type: str = Field(..., description="阈值类型")
    threshold_value: float = Field(..., description="阈值")
    comparison_operator: str = Field(..., description="比较操作符")
    alert_enabled: bool = Field(..., description="是否启用告警")
    created_at: datetime = Field(..., description="创建时间")
    last_triggered: Optional[datetime] = Field(None, description="最后触发时间")


class EnergyDashboardRequest(BaseModel):
    """能耗仪表板请求"""
    spaces: List[str] = Field(..., description="空间ID列表")
    time_range: str = Field(..., description="时间范围", pattern="^(1h|6h|24h|7d|30d)$")


class EnergyDashboardResponse(BaseModel):
    """能耗仪表板响应"""
    dashboard_id: str = Field(..., description="仪表板ID")
    spaces: List[str] = Field(..., description="空间列表")
    time_range: str = Field(..., description="时间范围")
    real_time_data: Dict[str, Any] = Field(..., description="实时数据")
    summary_statistics: Dict[str, Any] = Field(..., description="汇总统计")
    trend_charts: List[Dict[str, Any]] = Field(..., description="趋势图表")
    efficiency_rankings: List[Dict[str, Any]] = Field(..., description="效率排名")
    active_alerts: List[Dict[str, Any]] = Field(..., description="活跃告警")
    recommendations_preview: List[Dict[str, Any]] = Field(..., description="建议预览")
    generated_at: datetime = Field(..., description="生成时间")